export function uuid(len = 10) {
	let result = '';
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

	for (var i = 0; i < len; i++) {
		result += characters.charAt(Math.floor(Math.random() * characters.length));
	}

	return result;
}
export function sleep(time) {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve();
		}, time);
	});
}

export function closest(node, className) {
	let current = node;

	while (current && current.classList && !current.classList.contains(className)) {
		current = current.parentNode;
	}

	if (current === document) {
		return null;
	}

	return current;
}
export function queryPath(path) {
	const pagePath = path.split('?')[0];
	const paramStr = path.split('?')[1];
	const result = {
		query: {},
		pagePath,
	};

	if (!paramStr) {
		return result;
	}

	let paramList = paramStr.split('&');

	paramList.forEach((param) => {
		let key = param.split('=')[0];
		let value = param.split('=')[1];

		result.query[key] = value;
	});

	return result;
}
/**
 * json对象转url参数
 * @param data 对象
 */
export function parseParams(data) {
	var result = [];
	for (var key in data) {
		var value = data[key];
		if (value != null && value != "") {
			if (value.constructor == Array) {
				value.forEach(function (_value) {
					result.push(key + "=" + _value);
				});
			} else {
				result.push(key + '=' + value);
			}
		} else {
			result.push(key + '=' + value);
		}
	}
	return '?' + result.join('&');
}